<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Content-Language" content="zh-cn" />
    <meta name="author" content="xin.hu@socialcredits.cn" />
    <link href="/static/css/bootstrap.min.css" rel="stylesheet">
    <link href="/static/css/layoutit.css" rel="stylesheet">
    <script src="/static/js/bootstrap.min.js"></script>
    <script src="/static/js/jquery-ui.js"></script>
    <script src="/static/js/jquery.htmlClean.js"></script>
    <script src="/static/js/scripts.js"></script>
    <script src="/static/js/jquery-2.0.0.min.js"></script>
    <script src="/static/js/my.js"></script>
</head>
<body style="margin-right: 200px">
<input id="inputMessage" type="text" placeholder="Type something…" style="position:absolute;display: none;">
<div class="container-fluid" style="margin-bottom: 50px;">
    <div id="window" class="row-fluid">
        <div class="span12">
            <div id="tip" class="alert alert-info">
                <button type="button" class="close" data-dismiss="alert">×</button>
                <h4>提示!</h4>
                <strong>警告!</strong> 请注意你的个人隐私安全.
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/javascript">
    var websocket = null;

    //判断当前浏览器是否支持WebSocket
    if ('WebSocket' in window) {
        websocket = new WebSocket("ws://127.0.0.1:8099/ycyChat");
    }
    else {
        alert('Not support websocket')
    }

    //连接发生错误的回调方法
    websocket.onerror = function () {
        console.log("websocket open error")
    };

    //连接成功建立的回调方法
    websocket.onopen = function (event) {
        console.log("websocket open success")
    }

    //接收到消息的回调方法
    websocket.onmessage = function (event) {
        console.log(event.data);
        var json = eval("("+event.data+")");
        showMessageInNotice(json);
    }

    //连接关闭的回调方法
    websocket.onclose = function () {
        console.log("websocket close success")
    }

    //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
    window.onbeforeunload = function () {
        websocket.close();
    }

    //将消息显示在网页上
    function showMessageInNotice(json) {
        var str = '<div class="row-fluid" >' +
                '<div class="span12" style="float: '+json.textAlign+';">' +
                '<div class="alert alert-info" style="color:'+json.color+';background-color: '+json.background+';width: auto; ' +
                'text-align: left;">' +
                '<img width="30px" src="' + json.imgHeader + '">' +
                json.message +
                '</div></div></div>' +
                '<div style="clear:both"></div>';
        $("#window").append(str);
        $("#tip").css("background-color",json.background);
        $("#tip").css("color",json.color);
        window.scrollTo(0,document.body.scrollHeight);
    }

    //关闭连接
    function closeWebSocket() {
        websocket.close();
    }

    //发送消息
    function send(message) {
        websocket.send(message);
    }
</script>
</html>